package com.lenovo.manufacture;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AirConditioningControl extends AppCompatActivity {
    private Button btCold;
    private Button btHot;
    private Button btBack;
    private ImageButton btClose;
    private ImageView ivCold;
    private ImageView ivHot;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/UserFactory/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_air_conditioning_switch);
        initview();
        airConditionerClick();
    }

    private void initview() {
        btCold=findViewById(R.id.bt_cold);
        btHot =findViewById(R.id.bt_hot);
        btBack=findViewById(R.id.bt_back);
        btClose=findViewById(R.id.bt_close);
        ivCold=findViewById(R.id.iv_cold);
        ivHot=findViewById(R.id.iv_hot);
    }
    private void airConditionerClick(){
        btCold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                control(1,1);
            }
        });
        btHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                control(1,2);
            }
        });
        btClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    control(1,0);
            }
        });
        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
    private void control(int id,int acswitch){
        Service service=retrofit.create(Service.class);
        Call<AirConditionerData> call=service.airconditioner(id,acswitch);
       call.enqueue(new Callback<AirConditionerData>() {
           @Override
           public void onResponse(Call<AirConditionerData> call, Response<AirConditionerData> response) {
               ivCold.setImageResource(R.mipmap.cold);
               ivHot.setImageResource(R.mipmap.sun);
               int macswitch=response.body().getData().get(0).getAcSwitch();
               if (macswitch==1){
                   ivCold.setImageResource(R.mipmap.coldopen);
               }
               if (macswitch==2){
                   ivHot.setImageResource(R.mipmap.sunopen);
               }
           }

           @Override
           public void onFailure(Call<AirConditionerData> call, Throwable t) {

           }
       });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}